package per.jdz.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 *
 */
public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> futureTask = new FutureTask<>(new MyThread());
        new Thread(futureTask, "A").start();
        new Thread(futureTask, "B").start();
        // 会阻塞
        String s = futureTask.get();
        System.out.println(s);
        s = futureTask.get();
        System.out.println(s);
    }
}

class MyThread implements Callable<String> {

    @Override
    public String call() {
        System.out.println("call");
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "1234";
    }
}
